const router = require('koa-router')();
const logHelper = require('../utils/logHelper').helper;
const CurriculumService = require('../service/CurriculumService');

router.prefix('/Curriculum');


/**
 * @api {post} /Curriculum/AddCurriculum  新增课程
 * @apiGroup Curriculum
 * @apiName AddCurriculum
 * @apiParam {String} CurriculumName    课程名称
 * @apiParam {String} StageTitle    课程标题
 * @apiParam {String} CurriculumDescribe    课程描述
 * @apiParam {String} Price    价格(实际价格*100)
 * @apiParam {String} Token    Token值
 * @apiSuccess {String} code      (0参数不齐||1添加成功||2添加失败||3数据库报错||4服务器报错)
 */
router.post('/AddCurriculum', async (ctx, next) =>{
    let data=ctx.request.body;
    if(!data.CurriculumName||!data.StageTitle||!data.CurriculumDescribe||!data.Price){
        ctx.body={code:0,msg:'参数不齐'};
        return
    }
    logHelper.startLog('/Curriculum/AddCurriculum',data);
    try {
        let res=await CurriculumService.AddCurriculum(data);
        logHelper.endLog('/Curriculum/AddCurriculum',res);
        ctx.body=res;
    }catch (e) {
        logHelper.endErrLog('/Curriculum/AddCurriculum',e.message);
        ctx.body={code:4,msg:e.message}
    }
})



/**
 * @api {post} /Curriculum/QueryCurriculum  课程信息查询
 * @apiGroup Curriculum
 * @apiName QueryCurriculum
 * @apiParam {String} Token    Token值
 * @apiSuccess {String} code      (0参数不齐||1查询成功||2查询失败||3数据库报错||4服务器报错)
 * @apiSuccess {Array} data
 * @apiSuccess {String} data.TeacherUserName        讲师名
 * @apiSuccess {String} data.CurriculumId        课程ID
 * @apiSuccess {String} data.CurriculumName        课程名称
 * @apiSuccess {String} data.StageTitle        课程标题
 * @apiSuccess {String} data.TeacherUserId        教师ID
 * @apiSuccess {String} data.CurriculumDescribe        课程描述
 * @apiSuccess {Int} data.page        页码
 * @apiSuccess {Int} data.size        每页显示多少条
 * @apiSuccess {String} data.StageState        课程状态(0:未完成编辑、1:未授课、2:已授课、3:已结课)
 * @apiSuccess {String} data.Price        价格(实际价格需/100)
 * @apiSuccess {String} data.CreationTime        课程创建时间
 */
router.post('/QueryCurriculum', async (ctx, next) =>{
    let data=ctx.request.body;
    logHelper.startLog('/Curriculum/QueryCurriculum',data);
    try {
        let res=await CurriculumService.QueryCurriculum(data);
        logHelper.endLog('/Curriculum/QueryCurriculum',res);
        ctx.body=res;
    }catch (e) {
        logHelper.endErrLog('/Curriculum/QueryCurriculum',e.message);
        ctx.body={code:4,msg:e.message}
    }
})


/**
 * @api {post} /Curriculum/AddCurriculumDetails  添加课程详情
 * @apiGroup Curriculum
 * @apiName AddCurriculumDetails
 * @apiParam {String} Token    Token值
 * @apiParam {Array} CurriculumDetailsList    课程详情信息
 * @apiParam {String} CurriculumDetailsList.Section    第几节课
 * @apiParam {String} CurriculumDetailsList.SectionTitle    节课标题
 * @apiParam {String} CurriculumDetailsList.Duration    时长/分钟为单位
 * @apiParam {String} CurriculumDetailsList.OpeningHours    开课时间
 * @apiParam {Object} CurriculumData            课程表信息
 * @apiParam {String} CurriculumData.CurriculumId            课程表信息
 * @apiSuccess {String} code      (0参数不齐||1添加成功|2添加失败||3数据库报错||4服务器报错)
 */
router.post('/AddCurriculumDetails', async (ctx, next) =>{
    let data=ctx.request.body;
    if(!data.CurriculumData.CurriculumId||!data.CurriculumDetailsList[0].Section||!data.CurriculumDetailsList[0].SectionTitle||!data.CurriculumDetailsList[0].Duration||!data.CurriculumDetailsList[0].OpeningHours){
        ctx.body={code:0,msg:'参数不齐'};
        return
    }
    logHelper.startLog('/Curriculum/AddCurriculumDetails',data);
    try {
        let res=await CurriculumService.AddCurriculumDetails(data);
        logHelper.endLog('/Curriculum/AddCurriculumDetails',res);
        ctx.body=res;
    }catch (e) {
        logHelper.endErrLog('/Curriculum/AddCurriculumDetails',e.message);
        ctx.body={code:4,msg:e.message}
    }
})


/**
 * @api {post} /Curriculum/QueryCurriculumDetails  查询课程详情
 * @apiGroup Curriculum
 * @apiName QueryCurriculumDetails
 * @apiParam {String} Token    Token值
 * @apiParam {String} CurriculumId    课程表ID
 * @apiSuccess {String} code      (0参数不齐||1查询成功|2查询失败||3数据库报错||4服务器报错)
 * @apiSuccess {Array} data
 * @apiSuccess {String} data.CurriculumDetailsId            课程详情ID
 * @apiSuccess {String} data.CurriculumId            课程表ID
 * @apiSuccess {String} data.Section            节课数
 * @apiSuccess {String} data.SectionTitle            节课标题
 * @apiSuccess {String} data.Duration            时长/分钟为单位
 * @apiSuccess {String} data.EnrolmentNumber            报名人数
 * @apiSuccess {String} data.ListensNumber            收听人数
 * @apiSuccess {String} data.TeachingCondition            授课状态（0未授课，1已授课）
 * @apiSuccess {String} data.OpeningHours            开课时间
 */
router.post('/QueryCurriculumDetails', async (ctx, next) =>{
    let data=ctx.request.body;
    if(!data.CurriculumId){
        ctx.body={code:0,msg:'参数不齐'};
        return
    }
    logHelper.startLog('/Curriculum/QueryCurriculumDetails',data);
    try {
        let res=await CurriculumService.QueryCurriculumDetails(data);
        logHelper.endLog('/Curriculum/QueryCurriculumDetails',res);
        ctx.body=res;
    }catch (e) {
        logHelper.endErrLog('/Curriculum/QueryCurriculumDetails',e.message);
        ctx.body={code:4,msg:e.message}
    }
})


module.exports = router;
